import { apiToWithdrawInfo, apiToWithdraw } from '../../api/index'
import { getRoute } from '../../common/js/mixin'
let page = {
  /**
   * 页面的初始数据
   */
  data: {
    // 是否可申请
    isforward: false,
    // 是否超出金额
    isExport: true,
    // 提现说明
    isShow: false,
    // inputValue: 0,
    account: '',
    // 最低提现金额
    withdraw: 0
  },
  explain () {
    this.setData({
      isShow: true
    })
  },
  isClose () {
    this.setData({
      isShow: false
    })
  },
  // 提现记录
  toBring () {
    wx.navigateTo({
      url: '../bring-detail/bring-detail'
    })
  },
  // 选择提现方式
  changeAppli () {
    wx.navigateTo({
      url: '../select-account/select-account'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
  },
  // 页面数据
  getAppli () {
    apiToWithdrawInfo().then(
      res => {
        console.log(res)
        // 未开启提现
        if (res.data.config.is_use === 0) {
          wx.showModal({
            title: '提示',
            content: '商家未开启提现，请联系管理员!',
            showCancel: false,
            success: () => {
              wx.navigateBack({
                delta: 1
              })
            }
          })
          return
        }
        let data = res.data.account_list.find(item => item.is_default === 1)
        let account = ''
        if (data && data.account_type_name) {
          account = `${data.account_type_name}(${data.account_number})`
        }
        this.setData({
          appliInfo: res.data,
          account: account,
          withdraw: res.data.config.withdraw_cash_min - 0
        })
      }
    )
  },
  // 监听输入
  bindKeyInput (e) {
    let inputValue = e.hasOwnProperty('detail') ? e.detail.value : e
    // 是否超出金额
    let isExport = true
    let isforward = false
    if (inputValue > (this.data.appliInfo.balance - 0)) {
      isExport = false
    } else if (this.data.appliInfo.balance >= this.data.withdraw && inputValue >= this.data.withdraw) {
      isforward = true
    }
    this.setData({
      isExport,
      isforward,
      inputValue
    })
  },
  // 提交申请
  isTrue () {
    // 是否选择账号
    if (this.data.account && this.data.appliInfo.account_list[0].id) {
      if (this.data.appliInfo.balance >= this.data.withdraw && this.data.inputValue >= this.data.withdraw) {
        if ((this.data.appliInfo.user_first - 0) === 1) {
          wx.navigateTo({
            url: '../real-name/real-name'
          })
        } else {
          apiToWithdraw({ id: this.data.appliInfo.account_list[0].id, money: this.data.inputValue }).then(
            res => {
              wx.showToast({
                title: '提现成功',
                duration: 2000,
                success: () => {
                  const route = this.getRoute()
                  route.getPageInfo()
                  setTimeout(() => {
                    wx.navigateTo({
                      url: '../cash-audit/index'
                    })
                  }, 1000)
                }
              })
              this.getAppli()
            }
          )
        }
      } else {
        wx.showToast({
          title: this.data.appliInfo.config.withdraw_message,
          icon: 'none'
        })
      }
    } else {
      wx.showToast({
        title: '请选择提现账户',
        icon: 'none'
      })
    }
  },
  //  全部提现
  forwardAll () {
    this.bindKeyInput(this.data.appliInfo.balance - 0)
    if (this.data.inputValue < this.data.withdraw) {
      wx.showToast({
        title: `每笔提现金额不小于${this.data.withdraw}元`,
        icon: 'none',
        duration: 2000,
        mask: true
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    this.getAppli()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  }
}
getRoute(page)
Page(page)
